package com.athena.controller 
{
	import com.adobe.serialization.json.JSON;
	import com.athena.components.TextHolder;
	import com.athena.external.mail.CheckBox;
	import com.athena.external.mail.ItemMail;
	import com.athena.external.mail.MainMovie;
	import com.athena.utils.Delegate;
	import com.athena.utils.Util;
	import com.junkbyte.console.Cc;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	/**
	 * ...
	 * @author ...
	 */
	public class Mail extends MainMovie
	{
		private var active : String = "";
		private var tab : String = "";
		private var arrDel : Array = [];
		
		private var currentPage : int = 1;
		private var totalPage : int;
		public function Mail() 
		{
			var glow:GlowFilter = new GlowFilter();
			glow.color = 0x000000;
			glow.alpha = 0.5;
			glow.blurX = 10;
			glow.blurY = 10;
			glow.quality = BitmapFilterQuality.MEDIUM;

			filters=[glow];
			
			var _w : Number = width; var _h : Number = height;
			var rectangle:Shape = new Shape();
			rectangle.graphics.beginFill(0x000000, 1);
			rectangle.graphics.drawRect((_w - Main.getInstance().config.demension.x) / 2, (_h - Main.getInstance().config.demension.y) / 2, Main.getInstance().config.demension.x, Main.getInstance().config.demension.x);
			rectangle.alpha = 0.2;
			rectangle.graphics.endFill();
			addChild(rectangle);
			setChildIndex(rectangle, 0);
			
			this.x = (Main.getInstance().config.demension.x - _w) / 2;
			this.y = (Main.getInstance().config.demension.y - _h) / 2;
			
			setupButton();
			btnclick(null, "write");
		}
		
		private function setupButton() : void {
			var _tf : TextFormat = new TextFormat();
			_tf.font = "Tahoma";
			_tf.size = 14;
			_tf.color = 0x0099ff;
			
			var from : TextField = new TextHolder("Người nhận", _tf);
			from.name = "FROM";
			from.width = 185.55;
			from.height = 20.9;
			
			_tf.color = 0x666666;
			var ctn :  TextField = new TextHolder("Nội dung", _tf);
			ctn.name = "CONTENT";
			ctn.width = 185.55;
			ctn.height = 193.15;
			ctn.maxChars = 500;
			
			from.x = 3; from.y = 3.85;
			ctn.x = 3; ctn.y = 41.85;
			
			compose_mc.addChild(from);
			compose_mc.addChild(ctn);
			
			list_mc.pagination_mc.visible = false;
			
			system_btn.addEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "system"));
			system_btn.addEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "system"));
			system_btn.addEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "system"));
			
			player_btn.addEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "player"));
			player_btn.addEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "player"));
			player_btn.addEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "player"));
			
			write_btn.addEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "write"));
			write_btn.addEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "write"));
			write_btn.addEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "write"));
			
			close_btn.addEventListener(MouseEvent.CLICK, close);
			checkAll_mc.addEventListener(CheckBox.MAILCHECKBOX_CHECK, checkAllHandle);
			compose_mc.send_btn.addEventListener(MouseEvent.CLICK, sendMail);
			list_mc.delete_btn.addEventListener(MouseEvent.CLICK, sendDeleteMail);
			
			list_mc.pagination_mc.prev_btn.addEventListener(MouseEvent.CLICK, prevPage);
			list_mc.pagination_mc.next_btn.addEventListener(MouseEvent.CLICK, nextPage);
			
			detail_mc.reply_btn.addEventListener(MouseEvent.CLICK, reply);
		}
		
		private function removeEvent() : void {
			system_btn.removeEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "system"));
			system_btn.removeEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "system"));
			system_btn.removeEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "system"));
			
			player_btn.removeEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "player"));
			player_btn.removeEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "player"));
			player_btn.removeEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "player"));
			
			write_btn.removeEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "write"));
			write_btn.removeEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "write"));
			write_btn.removeEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "write"));
			
			close_btn.removeEventListener(MouseEvent.CLICK, close);
			checkAll_mc.removeEventListener(CheckBox.MAILCHECKBOX_CHECK, checkAllHandle);
			compose_mc.send_btn.removeEventListener(MouseEvent.CLICK, sendMail);
			list_mc.delete_btn.removeEventListener(MouseEvent.CLICK, sendDeleteMail);
			list_mc.pagination_mc.prev_btn.removeEventListener(MouseEvent.CLICK, prevPage);
			list_mc.pagination_mc.next_btn.removeEventListener(MouseEvent.CLICK, nextPage);
			
			detail_mc.reply_btn.addEventListener(MouseEvent.CLICK, reply);
		}
		
		public function MailPlayerRespone(obj : Object, admin : Boolean = false) : void {
			var p : int = obj.P;
			var array : Array = JSON.decode(obj.data);
			displayList(array, p, admin);
		}
		
		private function displayList(arr : Array, ttpage : int = 1, admin : Boolean = false) : void {
			totalPage = ttpage;
			while (list_mc.content_mc.numChildren > 0) {
				var m : MovieClip = MovieClip(list_mc.content_mc.getChildAt(0));
				m.removeEventListener(ItemMail.ITEM_CHECK, itemCheck);
				m.parent.removeChild(m);
				m = null;
			}
			
			for (var i : uint = 0; i < arr.length; i++ ) {
				var item_mc : ItemMail = new ItemMail(arr[i].Id, arr[i].Msg, arr[i].From, arr[i].To, arr[i].Time, arr[i].S, admin);
				item_mc.addEventListener(ItemMail.VIEWMAIL, Delegate.create(viewMail, item_mc.id, item_mc.read, item_mc.from, item_mc.content));
				item_mc.addEventListener(ItemMail.ITEM_CHECK, itemCheck);
				
				item_mc.x = 7;
				item_mc.y = i * 22.3;
				list_mc.content_mc.addChild(item_mc);
				if (i == 0) item_mc.click(null);
			}
			if (ttpage <= 1) list_mc.pagination_mc.visible = false;
			else {
				list_mc.pagination_mc.visible = true;
				list_mc.pagination_mc.text_txt.text = currentPage + " / " + totalPage;
			}
		}
		
		private function itemCheck(e : Event) : void {
			var len : int = list_mc.content_mc.numChildren;
			var y : Boolean = true;
			var b : Boolean = false;
			for (var i : int = 0 ; i < len; i++ ) {
				var item : ItemMail = ItemMail(list_mc.content_mc.getChildAt(i));
				if (!y || !item.Delete_chk.selected) {
					y = false;
				}
				if (b || item.Delete_chk.selected) {
					b = true;
				}
			}
			checkAll_mc.selected = y;
			list_mc.delete_btn.visible = b;
		}
		
		private function viewMail(e : Event, _id : int, _re : Boolean, fr : String, ctn : String) : void {
			if (!_re) {
				var obj :Object = new Object() ;
				obj.evt = "15" ;
				obj.T = 1 ;
				obj.ID = _id;
				Main.getInstance().socket.sendService(JSON.encode(obj)) ;
			}
			compose_mc.visible = false;
			detail_mc.visible = true;
			detail_mc.from_txt.text = fr;
			detail_mc.msg_txt.text = ctn;
			
			btnclick(null, tab);
		}
		
		private function prevPage(e : MouseEvent) : void {
			if (currentPage <= 1) return;
			currentPage -= 1
			var obj : Object = { };
			obj.evt = "15";
			obj.P = currentPage;
			obj.T = (tab == "system") ? 4 : 5;
			Main.getInstance().socket.sendService(JSON.encode(obj));
		}
		
		private function nextPage(e  :MouseEvent)  :void {
			if (currentPage >= totalPage) return;
			currentPage += 1;
			var obj : Object = { };
			obj.evt = "15";
			obj.P = currentPage;
			obj.T = (tab == "system") ? 4 : 5;
			Main.getInstance().socket.sendService(JSON.encode(obj));
		}
		
		private function reply(e : MouseEvent)  :void {
			btnclick(null, "write");
			(compose_mc.getChildByName("FROM") as TextField).text = detail_mc.from_txt.text;
		}
		private function sendDeleteMail(e : MouseEvent) : void {
			var len : int = list_mc.content_mc.numChildren;
			var arr : Array = [];
			var arrDel : Array = [];
			for (var i : int = 0 ; i < len; i++ ) {
				var item : ItemMail = ItemMail(list_mc.content_mc.getChildAt(i));
				if (item.Delete_chk.selected) {
					arr.push(item.id);
					arrDel.push(item);
				}
			}
			Util.simpleConfirm("Bạn có chắc chắn muốn xóa thư không!", Delegate.create(deleteMail,arr, arrDel));
			
		}
		
		private function deleteMail(arr : Array, arrDel : Array) : void {
			if (arr.length == 0) return;
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 3 ;
			obj.Arr = arr;
			Main.getInstance().socket.sendService(JSON.encode(obj)) ;
			for (var i : int = arrDel.length - 1; i >= 0; i-- ) {
				list_mc.content_mc.removeChild(arrDel[i]);
			}
			list_mc.delete_btn.visible = false;
			
			var len : int = list_mc.content_mc.numChildren;
			for (var j : int = 0; j < len; j++ ) {
				list_mc.content_mc.getChildAt(j).y = j * 22.3;
			}
			
		}
		private function sendMail(e : MouseEvent) : void {
			var ftxt : TextField = TextField(compose_mc.getChildByName("FROM"));
			var ctxt : TextField = TextField(compose_mc.getChildByName("CONTENT"));
			var from : String = ftxt.text;
			var ctn : String = ctxt.text;
			if (from == "") {
				Util.simpleAlert("Bạn chưa nhập tên người nhận!");
				Main.getInstance().stage.focus = ftxt;
				return;
			}
			if (ctn == "") {
				Util.simpleAlert("Bạn chưa nhập nội dung!");
				Main.getInstance().stage.focus = ctxt;
				return;
			}
			
			ftxt.text = ""; ctxt.text = "";
			var obj : Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 7 ;
			obj.NN = from ;
			obj.D = ctn;
			Main.getInstance().socket.sendService(JSON.encode(obj)) ;
			
		}
		
		private function checkAllHandle(e : Event) : void {
			if (list_mc.content_mc.numChildren == 0) {
				e.currentTarget.selected = false;
				return;
			}
			
			var num : int = list_mc.content_mc.numChildren - 1;
			arrDel = new Array;
			if (e.currentTarget.selected) {
				for (var i : int = 0; i <= num; i++ ) {
					(list_mc.content_mc.getChildAt(i) as ItemMail).Delete_chk.selected = true;
					arrDel.push((list_mc.content_mc.getChildAt(i) as ItemMail).id);
				}
				list_mc.delete_btn.visible = true;
			}else {
				for (var j : int = 0; j <= num; j++ ) {
					(list_mc.content_mc.getChildAt(j) as ItemMail).Delete_chk.selected = false;
					arrDel = new Array;
				}
				list_mc.delete_btn.visible = false;
			}
		}
		private function close(e : MouseEvent) : void {
			removeEvent();
			this.parent.removeChild(this);
		}
		private function btnover(e : MouseEvent, str: String) : void {
			Mouse.cursor = MouseCursor.BUTTON;
			if(active != str)
				(e.currentTarget as MovieClip).gotoAndStop(2);
		}
		private function btnout(e : MouseEvent, str: String) : void {
			Mouse.cursor = MouseCursor.AUTO;
			if(active != str)
				(e.currentTarget as MovieClip).gotoAndStop(1);
		}
	
		private function btnclick(e : MouseEvent, str : String) : void {
			if (active != str) {
				checkAll_mc.selected = false;
				list_mc.delete_btn.visible = false;
				active = str;
				system_btn.gotoAndStop(1);
				player_btn.gotoAndStop(1);
				write_btn.gotoAndStop(1);
				var btn : MovieClip;
				if (str == "system") btn = system_btn;
				else if (str == "player") btn = player_btn;
				else btn = write_btn;
				btn.gotoAndStop(3);
				
				var obj : Object = { };
				obj.evt = "15";
				obj.P = 1 ;
				switch(str) {
					case "system":
						obj.T = 4;
						tab = active;
						break;
					case "player":
						obj.T = 5;
						tab = active;
						break;
					case "write":
						detail_mc.visible = false;
						compose_mc.visible = true;
						break;
				}
				if (obj.T) {
					Main.getInstance().socket.sendService(JSON.encode(obj));
				}
			}
		}
	}

}